var graphWidth = 400;
var graphHeight = 30;
var graphMargin = 75;
var baseY = MainContainerHeight/2 - 100;
var graphX = MainContainerWidth -graphWidth -100;

Graph.prototype = new Sprite();
ScoreGraph.prototype = new Sprite();

function ScoreGraph (x,y,score,color){
    Sprite.apply(this);
    this.x = x;
    this.y = y;
    var maxLevelScore = 45;
    this.bg = new Sprite();
    this.bg.width = graphWidth;
    this.bg.height = graphHeight;
    this.bg.graphics.beginFill(white);
    this.bg.graphics.drawRect(0, 0, graphWidth, graphHeight);
    this.bg.graphics.beginFill(color);
    this.bg.graphics.drawRect(0, 0, graphWidth*(score/maxLevelScore),graphHeight);
    this.addChild(this.bg);
}

function Graph (score,player){
    Sprite.apply(this);
    
    this.Score = score;
    var tFormat = new TextFormat(font, 25, 0);
    tFormat.align = TextFormatAlign.CENTER;
    
    this.background = Game._game.bitmaps[Game.picturesPath+"base/menu.jpg"];
    this.addChild (this.background);
    
    this.InfScoreResult = new ScoreGraph (graphX,baseY-graphMargin,this.Score.infScore,blue);
    this.PhysScoreResult = new ScoreGraph (graphX,baseY,this.Score.physScore,yellow);
    this.MathScoreResult = new ScoreGraph (graphX,baseY+graphMargin,this.Score.mathScore,red);
    
    this.InfScoreLabel = null;
    this.InfScoreLabel = new TextField();    
    this.InfScoreLabel.setTextFormat(tFormat);
    this.InfScoreLabel.text = "Informatika";
    this.InfScoreLabel.width = this.InfScoreLabel.textWidth;
    this.InfScoreLabel.height = this.InfScoreLabel.textHeight;
    this.InfScoreLabel.x = graphX - MARGIN - this.InfScoreLabel.width;
    this.InfScoreLabel.y = baseY-graphMargin;

    this.PhysScoreLabel = null;
    this.PhysScoreLabel = new TextField();    
    this.PhysScoreLabel.setTextFormat(tFormat);
    this.PhysScoreLabel.text = "Fyzika"
    this.PhysScoreLabel.width = this.PhysScoreLabel.textWidth;
    this.PhysScoreLabel.height = this.PhysScoreLabel.textHeight;
    this.PhysScoreLabel.x = graphX - MARGIN - this.PhysScoreLabel.width;
    this.PhysScoreLabel.y = baseY;
    
    this.MathScoreLabel = null;
    this.MathScoreLabel = new TextField();    
    this.MathScoreLabel.setTextFormat(tFormat);
    this.MathScoreLabel.text = "Matematika";
    this.MathScoreLabel.width = this.MathScoreLabel.textWidth;
    this.MathScoreLabel.height = this.MathScoreLabel.textHeight;
    this.MathScoreLabel.x = graphX - MARGIN - this.MathScoreLabel.width;
    this.MathScoreLabel.y = baseY + graphMargin;
    
    this.congratulateLabel = new TextField();
    this.congratulateLabel.setTextFormat(tFormat);
    this.congratulateLabel.text = "Gratulujem "+Game._game.player.name + "!\n\
Ak sa chceš dovedieť viac o fakulte FMFI UK, \n\
klikni na jedno z nasledujucich tlačítok : ";
    this.congratulateLabel.width = this.congratulateLabel.textWidth;
    this.congratulateLabel.height = this.congratulateLabel.textHeight;
    this.congratulateLabel.x = (MainContainerWidth-this.congratulateLabel.width)/2;
    this.congratulateLabel.y = this.MathScoreLabel.y+this.MathScoreLabel.height+MARGIN*4;
    
    
    
    this.backToMenu = new Button("Späť do menu");
    this.backToMenu.x = MARGIN;   
    this.backToMenu.y = MainContainerHeight - MARGIN - this.backToMenu.height;
    this.backToMenu.addEventListener(MouseEvent.CLICK, clickBack);
    
    this.physButton = new Button("Pre uchádzačov");
    this.physButton.x = (MainContainerWidth - this.physButton.width)/2;
    this.physButton.y = this.congratulateLabel.y + this.congratulateLabel.height + 2*MARGIN;
    this.physButton.addEventListener(MouseEvent.CLICK, clickPhys);
    
    this.mathButton = new Button("Študijné programy");
    this.mathButton.x = this.physButton.x - this.mathButton.width - MARGIN;
    this.mathButton.y = this.congratulateLabel.y + this.congratulateLabel.height + 2*MARGIN;
    this.mathButton.addEventListener(MouseEvent.CLICK, clickMath);
    
    this.infButton = new Button("Stránka fakulty");
    this.infButton.x = this.physButton.x + this.physButton.width + MARGIN;
    this.infButton.y = this.congratulateLabel.y + this.congratulateLabel.height + 2*MARGIN;
    this.infButton.addEventListener(MouseEvent.CLICK, clickInf);
    
    
    this.addChild(this.InfScoreResult);
    this.addChild(this.PhysScoreResult);
    this.addChild(this.MathScoreResult);
    this.addChild(this.InfScoreLabel);
    this.addChild(this.PhysScoreLabel);
    this.addChild(this.MathScoreLabel);
    this.addChild(this.congratulateLabel);
    this.addChild(this.backToMenu);
    this.addChild (this.mathButton);
    this.addChild (this.infButton);
    this.addChild (this.physButton);
    
    this.updateText = updateText;
    
    function clickBack(e) {
        Game._game.LoadMainMenu();
        Game._game.LoadLevels();
        Game._game.score.restart();
    }
    
    function clickMath (e){
        var myWindow = window.open('http://www.fmph.uniba.sk/index.php?id=457', "_newtab");
        myWindow.focus();
    }
    
    function clickInf (e){
        var myWindow = window.open('http://fmph.uniba.sk', "_newtab");
        myWindow.focus();
    }
    
    function clickPhys (e){
        var myWindow = window.open('http://www.fmph.uniba.sk/index.php?id=2793', "_newtab");
        myWindow.focus();
    }
    
    
    var mScr = 'Matematika: '+ Math.floor(this.Score.mathScore/0.45)+ '%, ';
    var fScr =  'Fyzika: ' + Math.floor(this.Score.physScore/0.45) +  '%, ';
    var iScr = 'Informatika: ' + Math.floor(this.Score.infScore/0.45) + '%';
    
    FB.ui(
        {
            method: 'feed',
            name: 'Matfyz Game',
            link: 'https://apps.facebook.com/matfyzgame/',
            picture: 'fast-springs-7354.heroku.com/pictures/logo.gif',
            caption: "V\u00fdsledky hry:",
            description: (mScr+fScr+iScr)
        },
        function(response) {
            if (response && response.post_id) {
                console.log('Post was published.');
            } else {
                console.log('Post was not published.');
            }
        }
        );
    
}

function updateText (){
    this.InfScoreResultLabel.text = "VaÄąË‡e skÄ‚Ĺ‚re z informatickÄ‚Ëťch levelov: " + this.Score.infScore;
    this.PhysScoreResultLabel.text = "VaÄąË‡e skÄ‚Ĺ‚re z fyzikÄ‚Ë‡lnych levelov: " + this.Score.physScore;
    this.MathScoreResultLabel.text = "VaÄąË‡e skÄ‚Ĺ‚re z matematickÄ‚Ëťch levelov: " + this.Score.mathScore;
}